package org.springblade.task.job.report.sales;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.DateUtils;
import org.springblade.modules.salesAnalysis.entity.DailySalesTrendsReport;
import org.springblade.modules.salesAnalysis.service.IDailySalesTrendsReportService;
import org.springblade.task.base.param.BaseOrderBookParam;
import org.springblade.task.base.param.BaseParam;
import org.springblade.task.base.param.BaseUserParam;
import org.springblade.task.base.service.*;
import org.springblade.task.base.vo.*;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Title: 销售趋势
 * @version V1.0
 */
@Component
public class DailySalesTrendsReportJob {

	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;
	// VIP
	@Autowired
	private IBaseVipUserService baseVipUserService;
	@Autowired
	private IBaseVirtualMoneyFlowLogService baseVirtualMoneyFlowLogService;
	@Autowired
	private IBaseEbookOrderService baseEbookOrderService;
	@Autowired
	private IBaseAudioChapterOrderService baseAudioChapterOrderService;
	@Autowired
	private IBaseNetChapterOrderService baseNetChapterOrderService;
	@Autowired
	private IDailySalesTrendsReportService dailySalesTrendsReportService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("DailySalesTrendsReportJob")
	public ReturnT<String> DailySalesTrendsReportJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + DailySalesTrendsReportJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(DailySalesTrendsReportJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		String startTime = DateUtils.getDateStartTime(taskDate);
		String endTime = DateUtils.getDayEndTime(taskDate);
		List<DailySalesTrendsReport> salesTrendsReportList = new ArrayList<>();

		// VIP
		BaseUserParam userParam = new BaseUserParam();
		userParam.setStartDate(startTime);
		userParam.setEndDate(endTime);
		List<BaseVipUserVO> vipUserVOList = baseVipUserService.queryDailyServiceTypeVipUserList(userParam);
		DailySalesTrendsReport salesTrendsReport = null;
		for (BaseVipUserVO vipUserVO : vipUserVOList) {
			salesTrendsReport = new DailySalesTrendsReport();
			salesTrendsReport.setDate(taskDate);
			salesTrendsReport.setSaleType(1);
			salesTrendsReport.setSubType(vipUserVO.getServiceType());
			if (1 == vipUserVO.getServiceType()) {
				salesTrendsReport.setSaleAmount(vipUserVO.getServiceNum() * 1);
				salesTrendsReport.setSaleNum(vipUserVO.getServiceNum());
			} else if (1 == vipUserVO.getServiceType()) {
				salesTrendsReport.setSaleAmount(vipUserVO.getServiceNum() * 1);
				salesTrendsReport.setSaleNum(vipUserVO.getServiceNum());
			} else if (2 == vipUserVO.getServiceType()) {
				salesTrendsReport.setSaleAmount(vipUserVO.getServiceNum() * 30);
				salesTrendsReport.setSaleNum(vipUserVO.getServiceNum());
			} else if (3 == vipUserVO.getServiceType()) {
				salesTrendsReport.setSaleAmount(vipUserVO.getServiceNum() * 25);
				salesTrendsReport.setSaleNum(vipUserVO.getServiceNum());
			} else if (4 == vipUserVO.getServiceType()) {
				salesTrendsReport.setSaleAmount(vipUserVO.getServiceNum() * 88);
				salesTrendsReport.setSaleNum(vipUserVO.getServiceNum());
			} else if (5 == vipUserVO.getServiceType()) {
				salesTrendsReport.setSaleAmount(vipUserVO.getServiceNum() * 298);
				salesTrendsReport.setSaleNum(vipUserVO.getServiceNum());
			}

			salesTrendsReportList.add(salesTrendsReport);
		}

		// 充值
		BaseParam baseParam = new BaseParam();
		baseParam.setStartDate(startTime);
		baseParam.setEndDate(endTime);
		List<BaseVirtualMoneyFlowLogVO> virtualMoneyFlowLogVOList = baseVirtualMoneyFlowLogService.queryDailyVirtualMoneyFlowLogList(baseParam);
		for (BaseVirtualMoneyFlowLogVO virtualMoneyFlowLogVO : virtualMoneyFlowLogVOList) {
			salesTrendsReport = new DailySalesTrendsReport();
			salesTrendsReport.setDate(taskDate);
			salesTrendsReport.setSaleType(2);
			if (virtualMoneyFlowLogVO.getOsType() == 1) {
				salesTrendsReport.setOs("安卓");
				salesTrendsReport.setSubType(2);
			} else {
				salesTrendsReport.setOs("IOS");
				salesTrendsReport.setSubType(1);
			}
			salesTrendsReport.setSaleAmount(virtualMoneyFlowLogVO.getRechargeAmount());
			salesTrendsReport.setSaleNum(virtualMoneyFlowLogVO.getRechargeNum());
			salesTrendsReportList.add(salesTrendsReport);
		}

		BaseOrderBookParam orderBookParam = new BaseOrderBookParam();
		orderBookParam.setStartDate(startTime);
		orderBookParam.setEndDate(endTime);
//		int[] net =[34000000,34999999];
//		int[] audio = [36000000,37999999];
		// 电子书订单
		List<BaseEbookOrderVO> ebookOrderVOList = baseEbookOrderService.queryDailyOrderList(orderBookParam);
		Map<String, DailySalesTrendsReport> salesTrendsReportMap = new HashMap<>();
		for (BaseEbookOrderVO ebookOrderVO : ebookOrderVOList) {
			long ebookId = ebookOrderVO.getEbookId();
			String os = null;
			int subType = 0;
			int saleType;
			// 网文整本
			if (ebookId >= 34000000 && ebookId <= 34999999) {
				saleType = 4;
			} else if (ebookId >= 36000000 && ebookId <= 37999999) {
				saleType = 5;
			} else {
				saleType = 3;
			}

			// origin-来源，1-pcWeb，2-安卓，3-iphone，6-pad，14-pc客户端，15-阅读器
			if (1 == ebookOrderVO.getOrigin()) {
				os = "Web";
				subType = 4;
			} else if (2 == ebookOrderVO.getOrigin()) {
				os = "安卓";
				subType = 3;
			} else if (3 == ebookOrderVO.getOrigin()) {
				os = "IOS";
				subType = 2;
			} else if (6 == ebookOrderVO.getOrigin()) {
				os = "IOS";
				subType = 1;
			} else if (14 == ebookOrderVO.getOrigin()) {
				os = "PC端";
				subType = 4;
			} else if (15 == ebookOrderVO.getOrigin()) {
				os = "eink";
				subType = 4;
			}

			String key = os + "-" + saleType;
			DailySalesTrendsReport dailySalesTrendsReport = salesTrendsReportMap.get(key);
			if (null == dailySalesTrendsReport) {
				dailySalesTrendsReport = new DailySalesTrendsReport();
			}
			dailySalesTrendsReport.setDate(taskDate);
			dailySalesTrendsReport.setOs(os);
			dailySalesTrendsReport.setSubType(subType);
			dailySalesTrendsReport.setSaleType(saleType);
			dailySalesTrendsReport.setSaleAmount(dailySalesTrendsReport.getSaleAmount() + ebookOrderVO.getPayAmount());
			dailySalesTrendsReport.setSaleNum(dailySalesTrendsReport.getSaleNum() + 1);

		}

		// 有声订单
		List<BaseAudioChapterOrderVO> audioChapterOrderVOList = baseAudioChapterOrderService.queryDailyOrderList(orderBookParam);
		for (BaseAudioChapterOrderVO audioChapterOrderVO : audioChapterOrderVOList) {
			int saleType = 5;
			String os = audioChapterOrderVO.getOs();
			if ("ios".equals(os)) {
				os = "IOS";
			} else if ("android".equals(os)) {
				os = "安卓";
			} else if ("web".equals(os)) {
				os = "Web";
			} else if ("eink".equals(os)) {
				os = "eink";
			}

			String key = os + "-" + saleType;
			DailySalesTrendsReport dailySalesTrendsReport = salesTrendsReportMap.get(key);
			if (null == dailySalesTrendsReport) {
				dailySalesTrendsReport = new DailySalesTrendsReport();
			}
			dailySalesTrendsReport.setDate(taskDate);
			dailySalesTrendsReport.setOs(os);
//			dailySalesTrendsReport.setSubType(subType);
			dailySalesTrendsReport.setSaleType(saleType);
			dailySalesTrendsReport.setSaleAmount(dailySalesTrendsReport.getSaleAmount() + audioChapterOrderVO.getOnlineAmount());
			dailySalesTrendsReport.setSaleNum(dailySalesTrendsReport.getSaleNum() + 1);
		}

		// 网文订单
		List<BaseNetChapterOrderVO> netChapterOrderVOList = baseNetChapterOrderService.queryDailyOrderList(orderBookParam);
		for (BaseNetChapterOrderVO netChapterOrderVO : netChapterOrderVOList) {
			int saleType = 4;
			String os = netChapterOrderVO.getOs();
			netChapterOrderVO.getClient();
			if ("ios".equals(os)) {
				os = "IOS";
			} else if ("android".equals(os)) {
				os = "安卓";
			} else if ("web".equals(os)) {
				os = "Web";
			} else if ("eink".equals(os)) {
				os = "eink";
			}

			String key = os + "-" + saleType;
			DailySalesTrendsReport dailySalesTrendsReport = salesTrendsReportMap.get(key);
			if (null == dailySalesTrendsReport) {
				dailySalesTrendsReport = new DailySalesTrendsReport();
			}
			dailySalesTrendsReport.setDate(taskDate);
			dailySalesTrendsReport.setOs(os);
//			dailySalesTrendsReport.setSubType(subType);
			dailySalesTrendsReport.setSaleType(saleType);
			dailySalesTrendsReport.setSaleAmount(dailySalesTrendsReport.getSaleAmount() + netChapterOrderVO.getOnlineAmount());
			dailySalesTrendsReport.setSaleNum(dailySalesTrendsReport.getSaleNum() + 1);
		}

		Iterator it = salesTrendsReportMap.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next().toString();
			salesTrendsReportList.add(salesTrendsReportMap.get(key));
		}

		dailySalesTrendsReportService.saveBatch(salesTrendsReportList);
	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}

}
